package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsPermission;
import com.shengqiang.cloud.ups.dao.repository.master.UpmsPermissionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/27.
 * 菜单管理service
 */
@Service
public class UpmsMenuWebService extends ServiceImpl<UpmsPermissionMapper,UpmsPermission>{

    private static final Logger logger= LoggerFactory.getLogger(UpmsMenuWebService.class);

    public List<UpmsPermission> loadParentMenus(Map<String,Object> params){
        return baseMapper.loadMenusTree(params);
    }

    public JsonResult<UpmsPermission> addMenuTreeNode(UpmsPermission upmsPermission){
        JsonResult<UpmsPermission> result=new JsonResult<>(1,"fail");
        try{
            int count=baseMapper.insert(upmsPermission);
            if(count>0){
                result= new JsonResult<>(0,"success");
                result.setData(upmsPermission);
            }
        }catch (Exception ex){
            logger.error("添加菜单【"+ upmsPermission.getName()+"】出现异常:{}",ex.getMessage(),ex);
        }

        return result;
    }

    public JsonResult<List<UpmsPermission>> selectMemus(Map<String,Object> params){
        JsonResult<List<UpmsPermission>> result=null;
        try{
            List<UpmsPermission> upmsPermissions =baseMapper.selectMenus(params);
            if(upmsPermissions !=null){
                result=new JsonResult<>(0,"success");
                result.setData(upmsPermissions);
            }else{
                result=new JsonResult<>(1,"fail");
                result.setData(Lists.newArrayList());
            }
        }catch (Exception ex){
            logger.error("查询菜单列表信息出现异常:{}",ex.getMessage(),ex);
            result=new JsonResult<>(1,"fail");
        }
        return result;
    }

    /**
     * 修改菜单
     * @param upmsPermission
     * @return
     */
    public JsonResult updateMenu(UpmsPermission upmsPermission){
        JsonResult result=new JsonResult(1,"fail");
        try{
            int count=baseMapper.updateById(upmsPermission);
            if(count>0){
                return new JsonResult(0,"修改菜单【"+ upmsPermission.getName()+"】成功");
            }else{
                return new JsonResult(1,"修改菜单【"+ upmsPermission.getName()+"】出现异常");
            }
        }catch (Exception ex){
            logger.error("修改菜单【"+ upmsPermission.getName()+"】出现异常:{}",ex.getMessage(),ex);
            return new JsonResult(1,"修改菜单【"+ upmsPermission.getName()+"】出现异常");
        }
    }

    public JsonResult deleteMenuById(int id){
        try{
            int count=baseMapper.deleteById(id);
            if(count>0){
                return new JsonResult(0,"删除菜单成功");
            }else{
                return new JsonResult(1,"删除菜单失败");
            }
        }catch (Exception ex){
            logger.error("删除菜单出现异常:{}",ex.getMessage(),ex);
            return new JsonResult(1,"删除菜单出现异常");
        }
    }
}
